package com.juicygames.undoer.ballcounter;

import com.juicygames.framework.action.UpdateAction;
import com.juicygames.framework.draw.TextDisplay;
import com.juicygames.framework.exception.GamePartNotFound;
import com.juicygames.framework.gamepart.GameGroup;
import com.juicygames.framework.gamepart.GameHandler;
import com.juicygames.framework.gamepart.GamePart;
import com.juicygames.undoer.ball.Ball;
import com.juicygames.undoer.ball.BallGroup;

public class UpdateHandler extends GameHandler<UpdateAction> {
	
	private GamePart world;
	private GameGroup<Ball> ballGroup;
	private TextDisplay textDisplay;

	public UpdateHandler(GamePart self) {
		super(self);
		setActionClassHandled(UpdateAction.class);
		
		world = getWorld();
		textDisplay = self.getPart(TextDisplay.class);
		
	}

	public void handle(UpdateAction update) {
		
		if (ballGroup == null) {
			try {
				ballGroup = world.getPart(BallGroup.class);
			} catch (GamePartNotFound e) {}
		} else {
			int size = ballGroup.size();
			if (size > 1) {
				textDisplay.setText(ballGroup.size() + " circles");
			}
		}
		
	}
	
	

}
